/*
路由组
使用 middleware.JwtToken() 中间件进行 JWT 验证
其中 路由组 auth 要先进行验证才能访问
使用 middleware.Cors 函数解决 跨域问题
设置信任网络，减少性能损耗
*/

package routes

import (
    v1 "gitee.com/keeving/quanta-test-cat/api/v1"
    "gitee.com/keeving/quanta-test-cat/middleware"
    "gitee.com/keeving/quanta-test-cat/utils"
    "github.com/gin-contrib/multitemplate"
    "github.com/gin-gonic/gin"
)

func createMyRender() multitemplate.Renderer {
    p := multitemplate.NewRenderer()
    p.AddFromFiles("front", "web/front/dist/index.html")
    return p
}

func InitRouter() {
    gin.SetMode(utils.AppMode)
    r := gin.New()
    // 设置信任网络 []string
    // nil 为不计算，避免性能消耗，上线应当设置
    _ = r.SetTrustedProxies(nil)

    r.HTMLRender = createMyRender()
    r.Use(middleware.Logger())
    r.Use(gin.Recovery())
    r.Use(middleware.Cors())    // 跨域

    r.Static("/assets", "./web/front/dist/assets")
    r.StaticFile("/favicon.ico", "/web/front/dist/favicon.ico")

    r.GET("/", func(c *gin.Context) {
        c.HTML(200, "front", nil)
    })

    // 添加默认路由处理
    r.NoRoute(func(c *gin.Context) {
        c.HTML(200, "front", nil)
    })

    // 需要 token 登录验证的路由
    auth := r.Group("/")
    auth.Use(middleware.JwtToken())
    {
        // 用户模块的路由接口
        // auth.GET("admin/users", v1.GetUsers)
        // auth.PUT("user/:id", v1.EditUser)
        // auth.DELETE("user/:id", v1.DeleteUser)

        //修改密码
        // auth.PUT("admin/changepw/:id", v1.ChangeUserPassword)

        // 获取个人信息
        auth.GET("user/getInfo", v1.GetUserInfo)
        auth.POST("user/updateUserInfo", v1.UploadAvatar)
        auth.GET("user/getMyArticle", v1.GetUserArticles)
        auth.GET("user/getMyLove", v1.GetUserLikes)

        // 文章模块的路由接口
        auth.GET("admin/article/info/:id", v1.GetArtInfo)
        auth.DELETE("article/deleteArticle/:aid", v1.DeleteArt)
        auth.PUT("article/:id", v1.EditArt)

        // 上传文件
        auth.POST("upload", v1.UploadAvatar)

        // 评论模块
        auth.GET("comment/list", v1.GetCommentList)
        auth.GET("/article/deleteFirstComment/:firstCid", v1.DeleteComment)
        // auth.DELETE("delcomment/:id", v1.DeleteComment)
        // auth.PUT("checkcomment/:id", v1.CheckComment)
        // auth.PUT("uncheckcomment/:id", v1.UncheckComment)
        
        // 猫猫模块
        auth.POST("ketty/addKetty", v1.AddCat)
        auth.DELETE("ketty/deleteKetty/:kid", v1.DeleteCat)
        auth.POST("ketty/uploadImg", v1.UploadFileCat)

        // 文章模块
        auth.POST("article/publishArticle", v1.AddArticle)
        auth.GET("article/deleteArticle/:aid", v1.DeleteArt)

        // 评论模块
        auth.POST("article/publishFirstComment", v1.AddComment)
        auth.GET("article/likeFirstComment/:firstCid", v1.FirstCommentLike)
        
        // 点赞模块
        // auth.GET("user/getMyLove", v1.GetLike)
        auth.GET("article/likeArticle/:aid", v1.LikeArticle)
        
        // 猫猫医师 AI 模块
        auth.POST("utils/consultation", v1.AI)
    }

    /*
       前端展示页面接口
    */
    router := r.Group("/")
    {
        // 配置静态文件服务
        router.Static("/upload", utils.LocalUploadPath)

        // 登录控制模块
        router.POST("user/login/loginByPhone", v1.LoginFront)

        // 用户信息模块
        router.POST("user/login/signUp", v1.AddUser)

        // 退出验证
        router.GET("user/logout", v1.Logout)

        // 评论模块
        router.GET("article/getComments", v1.GetCommentListFront)

        // 文章模块
        router.GET("article/getArticles", v1.GetCateArt)    // token 权限
        router.GET("article/getArticle", v1.GetArtInfo)
        router.GET("article/searchArticles", v1.SearchArticle)

        // 猫猫模块
        router.GET("ketty/getKetties", v1.GetCats)
        router.GET("ketty/getKetty/:kid", v1.GetCat)
        router.GET("ketty/getKettyImgs/:kid", v1.GetCatImgs)
        router.GET("ketty/searchKetty", v1.SearchCat)
    }

    _ = r.Run(utils.HttpPort)
}
